package com.by.controller;

import com.by.bean.ResultBean;
import com.by.ex.BusinessException;
import com.by.ex.ValidateException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * date 2025/2/14 17:52
 * author bjj
 * decoration
 * version 1.0
 * since jdk11
 */
@RestControllerAdvice
public class GlobalExceptionController extends BaseController{
    @ExceptionHandler(com.by.ex.ValidateException.class)
    public ResultBean handlerValidationException(ValidateException e){
        return fail("验证失败",e.getErrorMap());
    }

    @ExceptionHandler(com.by.ex.BusinessException.class)
    public ResultBean handlerBusinessException(BusinessException e){
        return fail("业务失败",e.getMessage());
    }
    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public ResultBean handleDuplicateKeyException(DuplicateKeyException e) {
        return fail("业务失败", "该记录已存在，请勿重复添加");
    }
}
